Numbers & Dates in JavaScript

Visualizing the objects that handle numerical, mathematical, and temporal data.

Number

The standard numerical data type in JavaScript, representing a double-precision 64-bit binary format IEEE 754 value. This means it has a maximum safe integer value.

let safeInt = Number.MAX_SAFE_INTEGER;
let unsafeInt = safeInt + 1; // Unsafe

A standard number has a limit. Trying to go beyond it causes precision loss.


BigInt

A primitive data type used for integers with arbitrary precision. It can handle numbers larger than `Number.MAX_SAFE_INTEGER`.

const bigNum = 9007199254740991n + 1n; // Use 'n' for BigInt

Unlike a regular number, a BigInt can grow to any size, limited only by memory. It's like a calculator with infinite digits.


Math

A built-in object that provides a wide range of mathematical constants and functions, such as `random()`, `round()`, `abs()`, and `sin()`.

Math.round(4.7); // 5
Math.random(); // A number between 0 and 1

The `Math` object is like a toolbox filled with all the standard mathematical tools you'll ever need.

Enter a number:

Math.round():

Math.floor():

Math.random():


Date

A built-in object that represents a single moment in time. It is based on the number of milliseconds that have passed since the Unix Epoch (January 1, 1970 UTC).

const now = new Date();
console.log(now.toLocaleString());

The `Date` object is a timestamp. It captures the current moment and allows you to format and manipulate it.


Temporal

A new global object and API for dealing with dates and times in a modern, more precise way. It's a significant improvement over the legacy `Date` object.

const now = Temporal.Now.instant();
// Note: This API is still in Stage 3 and requires a polyfill.

The `Temporal` API provides a more structured and reliable way to handle time. Unlike `Date`, it clearly separates different concepts like time zones, dates, and times.

Temporal.Now.instant():

Temporal.Now.plainDateISO():

Note: Temporal API is not yet natively supported in all browsers.